package com.neusoft.ysu.populationmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.neusoft.ysu.populationmanagement.constant.TabelDic;
import com.neusoft.ysu.populationmanagement.entity.CommonResult;
import com.neusoft.ysu.populationmanagement.entity.ImpPop;
import com.neusoft.ysu.populationmanagement.mapper.ImpPopMapper;
import com.neusoft.ysu.populationmanagement.service.IImpPopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 作者：赵子鉴
 * @since 2024-06-18
 */
@Service
public class ImpPopServiceImpl implements IImpPopService {
    @Autowired
    ImpPopMapper impPopMapper;

    /**
     * @param impPop
     * @return com.neusoft.ysu.populationmanagement.entity.CommonResult
     * @author 赵子鉴
     * @description 重点人口录入
     * @date 2024-06-21 16:14
     */
    @Override
    public CommonResult insertImpPop(ImpPop impPop) {
        if (impPopMapper.selectByPopId(impPop.getPopId()).isEmpty()) {
            System.out.println("imp插入:" + impPop);
            impPop.setCreateTime(LocalDateTime.now());
            return new CommonResult<>(1, "重点人口信息插入成功", impPopMapper.insert(impPop));
        } else
            return new CommonResult<>(0, "插入失败,该条重点人口信息已存在", 0);
    }

    /**
     * @param impPop
     * @return com.neusoft.ysu.populationmanagement.entity.CommonResult
     * @author 赵子鉴
     * @description 重点人口信息更新
     * @date 2024-06-21 16:14
     */
    @Override
    public CommonResult updateImpPop(ImpPop impPop) {
        List<ImpPop> ipops = impPopMapper.selectByPopId(impPop.getPopId());
        if (ipops.size() == 0)
            return new CommonResult<>(0, "未检索到重点人口信息", 0);
        else if (ipops.size() > 1) {
            System.out.println("重点人口数据库错误-字段重复" + ipops);
            return new CommonResult<>(0, "重点人口数据库错误-字段重复", ipops);
        }

        int row = impPopMapper.updateByPopId(impPop);
        if (row < 1) {
            System.out.println("重点人口信息变更失败");
            return new CommonResult<>(0, "重点人口信息变更失败", row);
        }
        return new CommonResult<>(1, "重点人口信息变更成功", ipops.get(0));
    }

    /**
     * @param impPop ImpPop
     * @return com.neusoft.ysu.populationmanagement.entity.CommonResult
     * @author 赵子鉴
     * @description 重点人口注销（逻辑删除）
     * @date 2024-06-21 09:19
     */
    @Override
    public CommonResult deleteImp(ImpPop impPop) {
//        if (impPopMapper.selectByPopId(impPop.getPopId()).size() == 0)
//            return new CommonResult<>(0, "未检索到重点人口信息", 0);

        impPop.setIsDelete(1);
        // 将所有待审核的和已通过的重点人口信息逻辑删除
        int row = impPopMapper.deleteByPopId(impPop);
        if (row < 1) {
//            System.out.println("重点人口注销失败");
            return new CommonResult<>(0, "重点人口注销失败", row);
        }
        return new CommonResult<>(1, "重点人口注销成功，注销了 "+row+" 条数据", impPop);
    }

    @Override
    public CommonResult insertUncheckImpPop(ImpPop impPop) {
        if (impPopMapper.selectUncheckByPopId(impPop.getPopId()).isEmpty()) {
            System.out.println("uncheck_imp插入:" + impPop);
            impPop.setIsDelete(TabelDic.DELETE_UNCHECK);// 表示未审核
            impPop.setCreateTime(LocalDateTime.now());
            return new CommonResult<>(1, "重点人口信息变更提交成功", impPopMapper.insert(impPop));
        } else
            return new CommonResult<>(0, "插入失败,已存在变更申请", 0);
    }

    /**
     * @param impPop
     * @param delete
     * @return com.neusoft.ysu.populationmanagement.entity.CommonResult
     * @author 赵子鉴
     * @描述 重点人口变更请求-公安局局长审核
     * @date 2024-06-22 11:15
     */
    @Override
    public CommonResult finishCheckImpPop(ImpPop impPop, int delete) {
        List<ImpPop> ipops = impPopMapper.selectUncheckByPopId(impPop.getPopId());
        if (ipops.size() == 0)
            return new CommonResult<>(0, "未检索到待审核的重点人口信息", 0);
        else if (ipops.size() > 1) {
            System.out.println("重点人口数据库错误-待审核数据重复" + ipops);
            return new CommonResult<>(0, "重点人口数据库错误-待审核数据重复", ipops);
        }
        impPop.setIsDelete(delete);
        int row = impPopMapper.updateUncheckByPopId(impPop);
        if (row < 1) {
            System.out.println("重点人口信息变更失败");
            return new CommonResult<>(0, "重点人口信息变更失败", row);
        }
        return new CommonResult<>(1, "重点人口信息变更成功", ipops.get(0));
    }
    public CommonResult finishFinalCheckImpPop(ImpPop impPop, int delete) {
        List<ImpPop> ipops = impPopMapper.selectUnfinalByPopId(impPop.getPopId());
        if (ipops.size() == 0)
            return new CommonResult<>(0, "未检索到待审批的重点人口信息", 0);
        else if (ipops.size() > 1) {
            System.out.println("重点人口数据库错误-待审批数据重复" + ipops);
            return new CommonResult<>(0, "重点人口数据库错误-待审批数据重复", ipops);
        }
        impPop.setIsDelete(delete);
        // 查找出isdelete为0的数据
        List<ImpPop> origin=impPopMapper.selectByPopId(impPop.getPopId());
        for (ImpPop i:origin) {
            // 将所有的isdelete为0的数据逻辑删除
            i.setIsDelete(1);
            impPopMapper.updateByPopId(i);
        }
        int row = impPopMapper.updateUnfinalByPopId(impPop);
        if (row < 1) {
            System.out.println("重点人口信息变更失败");
            return new CommonResult<>(0, "重点人口信息变更失败", row);
        }
        return new CommonResult<>(1, "重点人口信息变更成功", ipops.get(0));
    }
}
